
import sys
import urllib2

def get_url_file(url, stream):
    """ get web page """
    try:
        response = urllib2.urlopen(url)
    except IOError, e:
        if hasattr(e, 'reason'):
            print 'network failure:', e.reason,
            m_status = 'ERROR - network failure - ' + str(e.reason)
        elif hasattr(e, 'code'):
            print 'web server error:', e.code,
            m_status = 'ERROR - web server error - ' + str(e.reason)
        else:
            print 'ERROR'
            m_status = 'ERROR - ' + str(e.reason)
    else:
        html = response.read()
        m_status = 'OK'
    if locals().has_key('html'):
        stream.write(html)
    return m_status

def isOnline(resource):
    """ check whethes is network available """
    try:
        response = urllib2.urlopen(resource)
    except:
        return False
    else:
        return True

if __name__ == "__main__":
    print isOnline(resource='http://www.google.com')
    print isOnline(r'file:///G:/AFT/docs/afttw.html')

    #o_fl = open('centrum.html', 'w')
    #sys.stdout.write('aaa')
    #get_url_file('http://www.centrum.cz', sys.stdout)
    #o_fl.close()
